package com.tensquare.user.service;

import com.tensquare.user.dao.AdminDao;
import com.tensquare.user.pojo.Admin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import util.IdWorker;

/**
 * @author Alfred
 * @description TODO
 * @date 2021/4/29 10:42
 */
@Service
public class AdminService {
    @Autowired
    private AdminDao adminDao;
    @Autowired
    private IdWorker idWorker;
    @Autowired
    private BCryptPasswordEncoder encoder;

    /**
     * fetch data by rule id
     *
     * @description  新增管理员密码加密
     * @param
     * @return
     */
    public void add(Admin admin){
        admin.setId(idWorker.nextId()+"");//设置主键
        //密码加密
        String newPassword = encoder.encode(admin.getPassword());
        admin.setPassword(newPassword);
        adminDao.save(admin);
    }

    /**
     * fetch data by rule id
     *
     * @description  根据登录名和密码校验
     * @param
     * @return
     */
    public Admin findByLoginnameAndPassword(String loginname,String password){
        Admin admin = adminDao.findByLoginname(loginname);
        if (admin!=null && encoder.matches(password,admin.getPassword())){
            return admin;
        }else {
            return null;
        }
    }
}
